<template>
  <van-button
    v-if="value"
    class="follow-btn"
    round
    size="small"
    :loading="followLoading"
    @click="fllowBtns"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="followLoading"
    @click="fllowBtns"
    >关注</van-button
  >
</template>

<script>
import { getFocus, deleteFocus } from '@/api/user.js'
export default {
  name: 'FocusUser',
  data() {
    return {
      followLoading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    // 点击关注或者取消关注按钮
    async fllowBtns() {
      this.followLoading = true // 开启按钮的loading状态
      try {
        if (this.value) {
          // 已关注状态 取消关注
          await deleteFocus(this.userId)
          // this.value = false
        } else {
          // 未关注状态 添加关注
          await getFocus(this.userId)
          // this.value = true
        }
        // 因为子组件不能直接修改传过来的数据 所以这里使用$emit方法传值到父组件修改数据
        // this.$emit('update-isArticleFllow', !this.value)
        this.$emit('input', !this.value)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast('不能关注自己')
        } else {
          this.$toast('操作失败请重试', err)
        }
      }
      this.followLoading = false // 关闭按钮的loading状态
    }
  }
}
</script>

<style scoped lang="less"></style>
